package com.dev;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import com.fb.FileBrowserPage;
import com.log;
import java.io.File;
import java.util.ArrayList;
import java.util.regex.Pattern;
public class Search extends LinearLayout implements OnClickListener,OnEditorActionListener{
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	FrameLayout.LayoutParams fllp=new FrameLayout.LayoutParams(0,0);
	FrameLayout.LayoutParams eklp;
	ExtraKey ek;
	TaskSearch ts;
	public File destination;
	EditText editor;
	SearchResult sr;
	FileBrowserPage fbp;
	FrameLayout parent;
	IconView close;
	TextView t1,t2,t3,t4,t5;
	LinearLayout titlebar;
	LinearLayout bottombar;
	LinearLayout searchDepth;
	TextButton tbSearch;
	IV2Bar ivadd,ivsub;
	CheckableItem ci1,ci2,ci3;
	int depth=8;
	MainActivity ma;
	public Search(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setWillNotDraw(false);
		wlp.format=PixelFormat.TRANSPARENT;
		close=new IconViewClose(ma);
		close.setOnClickListener(this);
		t1=new TextView(ma);
		t1.setText("搜索");
		t1.setTextColor(0xff000000);
		t1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
		t1.setGravity(Gravity.CENTER_VERTICAL);
		t1.setPadding(9,0,0,0);
		tbSearch=new TextButton(ma,"搜索");
		tbSearch.borderColor=FileBrowserPage.borderColor;
		tbSearch.borderWidth=FileBrowserPage.borderWidth;
		tbSearch.setOnClickListener(this);
		ci1=new CheckableItem(ma,"使用正则表达式");
		ci1.setOnClickListener(this);
		ci1.border=false;
		ci2=new CheckableItem(ma,"使用通配符");
		ci2.setOnClickListener(this);
		ci2.borderColor=FileBrowserPage.borderColor;
		ci2.borderWidth=FileBrowserPage.borderWidth;
		ci3=new CheckableItem(ma,"忽略大小写");
		ci3.setOnClickListener(this);
		ci3.borderColor=FileBrowserPage.borderColor;
		ci3.borderWidth=FileBrowserPage.borderWidth;
		ci3.box.setSelected(true);
		LinearLayout.LayoutParams bottombar_bt_lp=new LinearLayout.LayoutParams(0,-1,1);
		bottombar=new LinearLayout(ma);
		bottombar.setOrientation(HORIZONTAL);
		bottombar.addView(tbSearch,bottombar_bt_lp);
		t2=new TextView(ma);
		t2.setText("搜索深度");
		t2.setTextColor(0xff000000);
		t2.setTextSize(TypedValue.COMPLEX_UNIT_DIP,12);
		t2.setGravity(Gravity.CENTER_VERTICAL);
		t2.setPadding(9,0,0,0);
		ivadd=new IV2Bar(ma);
		ivadd.setOnClickListener(this);
		ivadd.verBar=true;
		ivsub=new IV2Bar(ma);
		ivsub.setOnClickListener(this);
		t3=new TextView(ma);
		t3.setText(depth>0?Integer.toString(depth):"∞");
		t3.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
		t3.setTextColor(0xff000000);
		t3.setGravity(Gravity.CENTER);
		t4=new TextView(ma);
		t4.setText("不限");
		t4.setTextColor(0xff00bbff);
		t4.setTextSize(TypedValue.COMPLEX_UNIT_DIP,12);
		t4.setGravity(Gravity.CENTER);
		t4.setPadding(20,0,20,0);
		t4.setBackground(new FileBrowserPage.Pressed());
		t4.setOnClickListener(this);
		t5=new TextView(ma);
		t5.setText("重置");
		t5.setTextColor(0xff00bbff);
		t5.setTextSize(TypedValue.COMPLEX_UNIT_DIP,12);
		t5.setGravity(Gravity.CENTER);
		t5.setPadding(20,0,20,0);
		t5.setBackground(new FileBrowserPage.Pressed());
		t5.setOnClickListener(this);
		searchDepth=new LinearLayout(ma);
		searchDepth.setOrientation(HORIZONTAL);
		searchDepth.addView(t2,new LayoutParams(0,-1,1));
		searchDepth.addView(ivsub,-2,-1);
		searchDepth.addView(t3,-2,-1);
		searchDepth.addView(ivadd,-2,-1);
		searchDepth.addView(t4,-2,-1);
		searchDepth.addView(t5,-2,-1);
		titlebar=new LinearLayout(ma){
			public void onDraw(Canvas cvs){
				super.onDraw(cvs);
				Paint p=new Paint();
				p.setColor(FileBrowserPage.borderColor);
				cvs.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
			}
		};
		titlebar.setWillNotDraw(false);
		titlebar.setOrientation(HORIZONTAL);
		titlebar.addView(t1,new LinearLayout.LayoutParams(0,-1,1));
		titlebar.addView(close);
		editor=new EditText(ma){
			Paint underline=new Paint();{
				underline.setColor(MainActivity.PURPLE);
				underline.setStrokeWidth(2);
			}
			@Override
			public void onDraw(Canvas cvs){
				super.onDraw(cvs);
				int yy=(getHeight()-getLineHeight())/2+getLineHeight();
				cvs.drawLine(getPaddingLeft()+getScrollX(),getPaddingTop()+yy,getWidth()-getPaddingRight()+getScrollX(),getPaddingTop()+yy,underline);
			}
		};
		editor.setBackgroundResource(0);
		editor.setSingleLine();
		editor.setImeOptions(EditorInfo.IME_ACTION_DONE);
		editor.setImeActionLabel("完成",editor.getImeActionId());
		editor.setOnEditorActionListener(this);
		editor.setHint("Empty");
		editor.setTextColor(0xff000000);
		editor.setTypeface(Typeface.MONOSPACE);
		editor.setTextSize(TypedValue.COMPLEX_UNIT_DIP,12);
		editor.setPadding(9,9,0,9);
		ek=new ExtraKey(ma);
		ek.destination=editor;
		eklp=new FrameLayout.LayoutParams(-1,(int)(30*ma.dp));
		eklp.gravity=Gravity.BOTTOM;
		addView(titlebar,-1,(int)(30*ma.dp));
		addView(editor,-1,(int)(30*ma.dp));
		addView(searchDepth,-1,(int)(30*ma.dp));
		addView(ci1,-1,(int)(25*ma.dp));
		addView(ci3,-1,(int)(25*ma.dp));
		addView(bottombar,-1,(int)(34*ma.dp));
		parent=new FrameLayout(ma);
		parent.addView(this,fllp);
		parent.addView(ek,eklp);
		parent.setBackgroundColor(0x00ffffff);
		ts=new TaskSearch();
	}
	public void show(FileBrowserPage fbp){
		this.fbp=fbp;
		fllp.width=fbp.getWidth()*4/5;
		fllp.height=-2;
		fllp.gravity=Gravity.CENTER;
		ma.wm.addView(parent,wlp);
	}
	void dismiss(){
		ma.wm.removeView(parent);
	}
	@Override
	public void onClick(View v){
		if(v==close){
			dismiss();
		}else if(v==ci1){
			if(ci1.box.isSelected()){
				ci1.box.setSelected(false);
			}else{
				ci1.box.setSelected(true);
			}
		}else if(v==ci3){
			if(ci3.box.isSelected()){
				ci3.box.setSelected(false);
			}else{
				ci3.box.setSelected(true);
			}
		}else if(v==ivadd){
			int depthWill=depth+1;
			if(depthWill<1){
				t3.setText(1+"");
				depth=1;
			}else{
				t3.setText(depthWill+"");
				depth=depthWill;
			}
		}else if(v==ivsub){
			int depthWill=depth-1;
			if(depthWill>0){
				depth=depthWill;
				t3.setText(depthWill+"");
			}else{
				depth=1;
				t3.setText(1+"");
			}
		}else if(v==t4){
			depth=-1;
			t3.setText("∞");
		}else if(v==t5){
			depth=1;
			t3.setText("1");
		}else if(v==tbSearch){
			String pattern=editor.getText().toString();
			if(pattern.length()<=0){
				Toast.makeText(ma,"输入有效的字符串",0).show();
			}else{
				dismiss();
				ts.regex=ci1.box.isSelected();
				if(ts.regex){
					try{
						Pattern.compile(pattern);
					}catch(Exception e){
						log.alert(e.toString());
						return;
					}
				}
				ts.depth=depth;
				ts.parent=destination;
				ts.pattern=pattern;
				ts.ignoreCase=ci3.box.isSelected();
				ts.show();
			}
		}
	}
	@Override
	public boolean onEditorAction(TextView p1,int p2,KeyEvent p3){
		return false;
	}
	@Override
	protected void onDraw(Canvas canvas){
		Paint p=new Paint();
		p.setColor(FileBrowserPage.borderColor);
		p.setStyle(Paint.Style.FILL);
		canvas.drawColor(0xffffffff);
		canvas.drawRect(0,0,FileBrowserPage.borderWidth,getHeight(),p);
		canvas.drawRect(0,0,getWidth(),FileBrowserPage.borderWidth,p);
		canvas.drawRect(getWidth()-FileBrowserPage.borderWidth,0,getWidth(),getHeight(),p);
		canvas.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
		super.onDraw(canvas);
	}
	public static class IV2Bar extends IconView{
		int color=MainActivity.CYAN;//0xff927272;
		public boolean verBar;
		public IV2Bar(MainActivity ma){
			super(ma);
			setBackground(new FileBrowserPage.Pressed());
		}
		@Override
		protected void onDraw(Canvas canvas){
			super.onDraw(canvas);
			Paint p=new Paint();
			p.setColor(color);
			p.setStrokeWidth(getHeight()/8);
			int padding=getHeight()/5;
			int x1=(getWidth()-getHeight())/2+padding;
			int x2=x1+getHeight()-padding*2;
			int y=getHeight()/2;
			canvas.drawLine(x1,y,x2,y,p);
			if(verBar){
				int x=getWidth()/2;
				int y1=padding;
				int y2=getHeight()-padding;
				canvas.drawLine(x,y1,x,y2,p);
			}
		}
	}
	class TaskSearch extends LinearLayout implements OnClickListener{
		private WindowManager.LayoutParams wlp=new WindowManager.LayoutParams(-1,-1);
		LinearLayout titlebar;
		LinearLayout bottombar;
		TextButton tbCancel;//tbHidden;
		TextView t1,t2;
		final String title="正在搜索";
		String pattern;
		File parent;
		TextView found;
		IconViewClose close;
		CheckableItem keepScreen;
		int depth=4;
		boolean regex;
		boolean ignoreCase;
		DoSearch find;
		volatile int fileFound=0;
		TaskSearch(){
			super(ma);
			wlp.type=wlp.TYPE_APPLICATION;
			wlp.format=PixelFormat.TRANSLUCENT;
			wlp.flags=0;
			setOrientation(VERTICAL);
			setWillNotDraw(false);
			close=new IconViewClose(ma);
			close.setOnClickListener(this);
			t1=new TextView(ma);
			t1.setTextColor(0xff000000);
			t1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
			t1.setGravity(Gravity.CENTER_VERTICAL);
			t1.setPadding(9,0,0,0);
			found=new TextView(ma);
			found.setTextColor(0xff000000);
			found.setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
			found.setGravity(Gravity.CENTER_VERTICAL);
			found.setPadding(9,0,0,0);
			t2=new TextView(ma);
			t2.setTextColor(0xff000000);
			t2.setTextSize(TypedValue.COMPLEX_UNIT_DIP,12);
			t2.setGravity(Gravity.TOP);
			t2.setPadding(9,0,0,0);
			t2.setMaxLines(3);
			titlebar=new LinearLayout(ma){
				public void onDraw(Canvas cvs){
					super.onDraw(cvs);
					Paint p=new Paint();
					p.setColor(FileBrowserPage.borderColor);
					cvs.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
				}
			};
			titlebar.setWillNotDraw(false);
			titlebar.setOrientation(HORIZONTAL);
			titlebar.addView(t1,new LinearLayout.LayoutParams(0,-1,1));
			titlebar.addView(close);
			keepScreen=new CheckableItem(ma,"保持屏幕常亮");
			keepScreen.borderColor=FileBrowserPage.borderColor;
			keepScreen.setOnClickListener(this);
			//LinearLayout.LayoutParams bottombar_bt_lp=new LinearLayout.LayoutParams(0,-1,1);
			bottombar=new LinearLayout(ma);
			bottombar.setOrientation(HORIZONTAL);
			addViewInLayout(titlebar,-1,new LayoutParams(-1,(int)(30*ma.dp)));
			addViewInLayout(found,-1,new LayoutParams(-1,-2));
			addViewInLayout(t2,-1,new LayoutParams(-1,(int)(45*ma.dp)));
			addViewInLayout(keepScreen,-1,new LayoutParams(-1,(int)(25*ma.dp)));
		}
		void show(){
			wlp.width=fbp.getWidth()*3/4;
			wlp.height=-2;
			setKeepScreenOn(false);
			keepScreen.box.setSelected(false);
			t1.setText(title);
			ma.wm.addView(this,wlp);
			find=new DoSearch();
			find.start();
		}
		void dismiss(){
			ma.wm.removeView(this);
		}
		volatile SearchResult sr;
		@Override
		public void onClick(View v){
			if(v==close){
				if(find!=null){
					find.cancel=true;
					int size=sr.results.size();
					if(size>0){
						File fs[]=new File[size];
						sr.results.toArray(fs);
						fbp.setWillShow(fs);
					}
				}
				dismiss();
			}else if(v==keepScreen){
				if(keepScreen.box.isSelected()){
					keepScreen.box.setSelected(false);
					setKeepScreenOn(false);
				}else{
					keepScreen.box.setSelected(true);
					setKeepScreenOn(true);
				}
			}
		}
		@Override
		protected void onDraw(Canvas canvas){
			Paint p=new Paint();
			p.setColor(fbp.borderColor);
			p.setStyle(Paint.Style.FILL);
			canvas.drawColor(0xffffffff);
			canvas.drawRect(0,0,fbp.borderWidth,getHeight(),p);
			canvas.drawRect(0,0,getWidth(),fbp.borderWidth,p);
			canvas.drawRect(getWidth()-fbp.borderWidth,0,getWidth(),getHeight(),p);
			canvas.drawRect(0,getHeight()-fbp.borderWidth,getWidth(),getHeight(),p);
			super.onDraw(canvas);
		}
		class DoSearch extends Thread{
			volatile boolean cancel=false;
			volatile boolean notifyResult=false;
			void search(File[] fs,int currentDepth,final String s){
				if(cancel){
					return;
				}
				if(depth>0){
					if(currentDepth>depth){
						return;
					}
				}
				if(notifyResult){
					int size=sr.results.size();
					final File f[]=new File[size];
					sr.results.toArray(f);
					post(new Runnable(){
							@Override
							public void run(){
								fbp.setWillShow(f);
								t2.setText(s);
								found.setText("已找到"+fileFound+"项");
							}
						});
					notifyResult=false;
				}
				for(int ii=0,lim=fs.length;ii<lim;ii++){
					File f=fs[ii];
					final String name=f.getName();
					if(regex){
						if(name.matches(pattern)){
							sr.results.add(f);
							fileFound+=1;
						}
					}else if(ignoreCase){
						if(name.toUpperCase().contains(pattern)){
							sr.results.add(f);
							fileFound+=1;
						}
					}else{
						if(name.contains(pattern)){
							sr.results.add(f);
							fileFound+=1;
						}
					}
					if(f.isDirectory()){
						if(f.canRead()){
							search(f.listFiles(),currentDepth+1,s+'/'+name);
							//names.remove(names.size()-1);
						}
					}
				}
			}
			@Override
			public void run(){
				fileFound=0;
				if(ignoreCase)	{
					pattern=pattern.toUpperCase();
				}
				sr=new SearchResult();
				File list[]=parent.listFiles();
				Thread update=new Thread(){
					@Override
					public void run(){
						try{
							while(!isInterrupted()){
								sleep(160);
								notifyResult=true;
							}
						}catch(InterruptedException e){}
					}
				};
				update.start();
				search(list,1,parent.getName());
				//log.alert(onSearch);
				update.interrupt();
				if(cancel)return;
				post(new Runnable(){
					@Override 
					public void run(){
						t1.setText("搜索完成");
						int size=sr.results.size();
						if(size>0){
							File f[]=new File[size];
							sr.results.toArray(f);
							fbp.setWillShow(f);
						}
					}
				});
			}
		}
	}
	class SearchResult{
		ArrayList<File> results=new ArrayList<File>();
		String keyWord;
		boolean regex;
	}
}
